;(function () {
  let $modal = $('#admin-log-modal')
  let $infoTable = $modal.find('#info-table')
  let $requestTable = $modal.find('#request-table')

  let $viewSpan = $('.log-view')

  let fnTrigger = () => {
    $viewSpan.click(function () {
      console.log(1)
      let $this = $(this)
      let data = {
        id: $this.attr('data-id'),
        admin_id: $this.attr('data-admin-id'),
        admin_signname: $this.attr('data-admin-signname'),
        rid: $this.attr('data-rid'),
        ip: $this.attr('data-ip'),
        create_time: $this.attr('data-create-time'),
        title: $this.attr('data-title'),
        url: $this.attr('data-url')
      }
      let request = $this.attr('data-request')
      try {
        request = $.parseJSON(request)
      } catch(e) {
        request = {}
      }
      fnInitTable($infoTable, data)
      fnInitTable($requestTable, request)
    })
  }

  let fnInitInfoTable = (data) => {
    $tbody = $infoTable.find('tbody')
    $tbody.children().remove()
    $trs = $('')
    for (let key in data) {
      $trs.append('<tr><td>' + key + '</td><td>' + data[key] + '</td></tr>')
    }
    $infoTable.append($trs)
  }

  let fnInitRequestTable = (data) => {
    $tbody = $requestTable.find('tbody')
    $tbody.children().remove()
    $trs = $('')
    for (let key in data) {
      $trs.append('<tr><td>' + key + '</td><td>' + data[key] + '</td></tr>')
    }
    $requestTable.append($trs)
  }

  let fnInitTable = function ($table, data) {
    // $tbody = $table.find('tbody')
    // $tbody.children().remove()
    $table.children('tbody').remove();
    $trs = $('<tbody></tbody>')
    for (let key in data) {
      $trs.append('<tr><td>' + key + '</td><td>' + data[key] + '</td></tr>')
    }
    console.log($trs)
    // $tbody.append($trs)
    $table.append($trs)
  }

  fnTrigger()
})()
